//
//  KnowMoreSCell.m
//  NewPro
//
//  Created by 何荧红 on 2019/1/16.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "KnowMoreSCell.h"

@interface KnowMoreSCell ()
@property (weak, nonatomic) IBOutlet UIImageView *Typeimg;
@property (weak, nonatomic) IBOutlet UILabel *TypeTitle;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *PushHeight;
@property (weak, nonatomic) IBOutlet UIImageView *PushImg;
@property (weak, nonatomic) IBOutlet UIView *ContView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *ContHeight;

@end

@implementation KnowMoreSCell
- (void)setModel:(KnowMoreSecmodel *)model{
    _model = model;
    if (self.Search.length > 0) {
        if (_model.problems.count > 0) {
            NSMutableArray *subWitharr = [[NSMutableArray alloc]initWithArray:_model.problems copyItems:YES];
            for (NSInteger i = 0 ; i < _model.problems.count; i++) {
                NSDictionary *dic = _model.problems[i];
                if (([_model.group rangeOfString:self.Search].location != NSNotFound) || ([dic[@"title"] rangeOfString:self.Search].location != NSNotFound) || ([dic[@"content"] rangeOfString:self.Search].location != NSNotFound)) {
                    NSMutableDictionary * subdic = [[NSMutableDictionary alloc]initWithDictionary:dic copyItems:YES];
                    if ([_model.group rangeOfString:self.Search].location != NSNotFound) {
                        NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:_model.group];
                        [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[_model.group rangeOfString:self.Search]];
                        self.TypeTitle.attributedText = string;
                    }
                    if ([dic[@"title"] rangeOfString:self.Search].location != NSNotFound) {
                        NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:dic[@"title"]];
                        [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[dic[@"title"] rangeOfString:self.Search]];
                        [subdic setObject:string forKey:@"title"];
                    }
                    if ([dic[@"content"] rangeOfString:self.Search].location != NSNotFound) {
                        NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:dic[@"content"]];
                        [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[dic[@"content"] rangeOfString:self.Search]];
                        [subdic setObject:string forKey:@"content"];
                    }
                    [subWitharr replaceObjectAtIndex:i withObject:subdic];
                }
            }
            _model.problems = subWitharr;
        }else{
            if ([_model.group rangeOfString:self.Search].location != NSNotFound) {
                NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:_model.group];
                [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[_model.group rangeOfString:self.Search]];
                self.TypeTitle.attributedText = string;
            }else{
                self.TypeTitle.attributedText = nil;
                self.TypeTitle.text = _model.group;
            }
        }
    }else{
        self.TypeTitle.attributedText = nil;
        self.TypeTitle.text = _model.group;   
    }
    if (_model.problems.count <= 2) {
        self.PushImg.hidden = YES;
    }else{
        self.PushImg.hidden = NO;
    }
    [self.Typeimg sd_setImageWithURL:[NSURL URLWithString:_model.icon]];
    self.ContHeight.constant = _model.problems.count * 45;
    if (self.ContView.subviews.count > 0) {
        for (UIView *sub in self.ContView.subviews) {
            [sub removeFromSuperview];
        }
    }
    for (NSInteger i = 0; i < _model.problems.count; i++) {
        UIButton *button = [[UIButton alloc]init];
        button.tag = i;
        [button addTarget:self action:@selector(DetailViewPush:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:12];
        [button setTitleColor:[UIColor colorWithHexString:@"666666"] forState:UIControlStateNormal];
        if ([_model.problems[i][@"title"] isKindOfClass:[NSMutableAttributedString class]]) {
            [button setAttributedTitle:_model.problems[i][@"title"] forState:UIControlStateNormal];
        }else{
            [button setTitle:_model.problems[i][@"title"] forState:UIControlStateNormal];
        }
        [self.ContView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.ContView.mas_left).offset(14);
            make.right.equalTo(self.ContView.mas_right);
            make.top.equalTo(self.ContView.mas_top).offset(45 * i);
            make.height.mas_equalTo(45);
        }];
        if (i >= 2) {
            button.hidden = YES;
        }else{
            button.hidden = NO;
        }
    }
    UIView *line = [[UIView alloc]init];
    line.tag = 234;
    line.backgroundColor = [UIColor colorWithHexString:@"cccccc"];
    [self.ContView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ContView.mas_top).offset(45);
        make.left.equalTo(self.ContView.mas_left).offset(20);
        make.right.equalTo(self.ContView.mas_right);
        make.height.mas_equalTo(1);
    }];
    if (_model.problems.count > 2) {
        for (NSInteger j = 1; j < _model.problems.count - 1; j++) {
            UIView *line = [[UIView alloc]init];
            line.tag = 234 + j;
            line.backgroundColor = [UIColor colorWithHexString:@"cccccc"];
            [self.ContView addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.ContView.mas_top).offset(45 * (j + 1));
                make.left.equalTo(self.ContView.mas_left).offset(20);
                make.right.equalTo(self.ContView.mas_right);
                make.height.mas_equalTo(1);
            }];
        }
    }
}
- (void)DetailViewPush:(UIButton *)button{
    if (self.ViewPushBlock) self.ViewPushBlock(button.tag);
}
- (void)setIsPush:(BOOL)isPush{
    _isPush = isPush;
    if (_isPush) {
        self.PushImg.image = [UIImage imageNamed:@"灰色上箭头"];
        if (_model.problems.count <= 2) {
            self.ContHeight.constant = 90;
        }else{
            self.ContHeight.constant = 45 * _model.problems.count;
        }
        if (_model.problems.count > 2) {
            for (UIView *sub in self.ContView.subviews) {
                sub.hidden = NO;
            }
        }
    }else{
        self.PushImg.image = [UIImage imageNamed:@"灰色下箭头"];
        self.ContHeight.constant = 90;
        if (_model.problems.count > 2) {
            for (UIButton *sub in self.ContView.subviews) {
                if ((sub.tag >= 2 && sub.tag != 234) || sub.tag > 235) {
                    sub.hidden = YES;
                }else{
                    sub.hidden = NO;
                }
            }
        }
        
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
}
- (IBAction)WilltoPush:(UIButton *)sender {
    self.isPush = !self.isPush;
    if (self.SendPushBlock) self.SendPushBlock(self.isPush);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
